﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI.WebControls;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.LocationDocument
{
    public partial class View : BasePage_BackEnd
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pager.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);
            if (!IsPostBack)
            {
                pager.CurrentPageIndex = 0;
                PagerControll_PageChange(pager.CurrentPageIndex);
                ddlLocation.DataSource = LocationPresent.GetList();
                ddlLocation.DataTextField = "Name_CN";
                ddlLocation.DataValueField = "LocationId";
                ddlLocation.DataBind();
                ddlLocation.Items.Insert(0, new ListItem(CommonResource.PleaseSelect, "-1"));
            }
        }

        public override void InitControlResource()
        {
            int i = 0;
            gvLocationDocument.Columns[i++].HeaderText = NameResource.No;
            gvLocationDocument.Columns[i++].HeaderText = ManageResource.LocationDocument_Location;
            gvLocationDocument.Columns[i++].HeaderText = ManageResource.LocationDocument_NameCN;
            gvLocationDocument.Columns[i++].HeaderText = ManageResource.LocationDocument_NameEN;
            gvLocationDocument.Columns[i++].HeaderText = ManageResource.LocationDocument_CreateBy;
            gvLocationDocument.Columns[i++].HeaderText = ManageResource.LocationDocument_LastModifyTime;
            gvLocationDocument.Columns[i++].HeaderText = ManageResource.mgr_lblAction;
            btnAdd.Text = ManageResource.LocationDocument_AddDocument;
            btnQuery.Text = ManageResource.mgr_btnQuery;
            lblFromDate.Text = ManageResource.LocationDocument_LastModifyTime;
            lblLocation.Text = ManageResource.LocationDocument_Location;
            lblNameCN.Text = ManageResource.LocationDocument_NameCN;
            lblNameEN.Text = ManageResource.LocationDocument_NameEN;
            lblOverError.Text = string.Empty;
            revtxtFromDate.ErrorMessage = CommonResource.ErrorFormat;
            revtxtToDate.ErrorMessage = CommonResource.ErrorFormat;
        }

        protected void PagerControll_PageChange(int pageIndex)
        {
            string language = Language.ToString();
            var dataPagerSource = LocationDocumentPresent.GetPaged(pageIndex, pager.PageSize, GetLambaCondition(), p => p.LocationDocumentId, true);
            gvLocationDocument.DataSource = dataPagerSource;
            gvLocationDocument.DataBind();
            var count = 0;
            count = dataPagerSource.TotalCount;
            pager.IntialProperties(count);
        }

        private Expression<Func<Model.LocationDocument, bool>> GetLambaCondition()
        {
            Expression<Func<Model.LocationDocument, bool>> searchExp = p => true;
            string language = string.Empty;
            int locationid = 0;
            if (int.TryParse(ddlLocation.SelectedValue,out locationid))
            {
                if (locationid > 0)
                {
                    searchExp = searchExp.AndPredicate<Model.LocationDocument>(p => p.LocationId == locationid);
                }               
            }
            if (txtNameCN.Text != null && txtNameCN.Text.Trim() != string.Empty)
            {
                var namecn = txtNameCN.Text.Trim();
                searchExp = searchExp.AndPredicate<Model.LocationDocument>(p => p.Name_CN.Contains(namecn));
            }
            if (txtNameEN.Text != null && txtNameEN.Text.Trim() != string.Empty)
            {
                var nameen = txtNameEN.Text.Trim();
                searchExp = searchExp.AndPredicate<Model.LocationDocument>(p => p.Name_EN.Contains(nameen));
            }
            if (txtFromDate.Text != null && txtFromDate.Text.Trim() != string.Empty)
            {
                try
                {
                    var fromDate = Convert.ToDateTime(txtFromDate.Text);
                    searchExp = searchExp.AndPredicate<Model.LocationDocument>(p => p.LastModifyTime >= fromDate);
                }
                catch { }
            }
            if (txtToDate.Text != null && txtToDate.Text.Trim() != string.Empty)
            {
                try
                {
                    var toDate = Convert.ToDateTime(txtToDate.Text);
                    DateTime overtoDate = toDate.AddDays(1);
                    searchExp = searchExp.AndPredicate<Model.LocationDocument>(p => p.LastModifyTime < overtoDate);
                }
                catch { }
            }
            return searchExp;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            lblOverError.Text = string.Empty;
            if (!string.IsNullOrEmpty(txtFromDate.Text) && !string.IsNullOrEmpty(txtToDate.Text))
            {
                var fromDate = Convert.ToDateTime(txtFromDate.Text);
                var toDate = Convert.ToDateTime(txtToDate.Text);
                if (fromDate > toDate)
                {
                    lblOverError.Text = "开始日期小于结束日期" + "！";
                    return;
                }
            }
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("/LocationDocument/Update.aspx?pagestatus=new");
        }

        protected void gvLocationDocument_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Model.LocationDocument locationDocument = e.Row.DataItem as Model.LocationDocument;
                (e.Row.Cells[0].FindControl("lblNum") as Label).Text = (e.Row.RowIndex + 1 + (pager.CurrentPageIndex) * pager.PageSize).ToString();
                var lbtnDelete = e.Row.Cells[6].FindControl("lbtnDelete") as LinkButton;
                var lbtnUpdate = e.Row.Cells[6].FindControl("lbtnUpdate") as LinkButton;
                lbtnDelete.Text = ManageResource.mgr_btnDelete;
                lbtnUpdate.Text = ManageResource.mgr_btnEdit;
                lbtnDelete.Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                var user = UserPresent.GetById(locationDocument.CreateBy);
                if (user != null)
                    (e.Row.Cells[4].FindControl("lblCreateBy") as Label).Text = user.NameCN;
                var location = LocationPresent.GetById(locationDocument.LocationId);
                if (location != null)
                    (e.Row.Cells[1].FindControl("lblLocation") as Label).Text = location.Name_CN;
            }
        }

        protected void gvLocationDocument_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = e.RowIndex;
            int locationDocumentId = (int)this.gvLocationDocument.DataKeys[i]["LocationDocumentId"];
            LocationDocumentPresent.Delete(locationDocumentId);
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void gvLocationDocument_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = e.RowIndex;
            int locationDocumentId = (int)this.gvLocationDocument.DataKeys[i]["LocationDocumentId"];
            Response.Redirect("/LocationDocument/Update.aspx?pagestatus=edit&Id=" + locationDocumentId);
        }
    }
}